﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using static ParkSaleForAdmin.Data.SuUserRequestInfo;
using static ParkSaleForAdmin.SystemManager;

namespace ParkSaleForAdmin
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            Application.Current.Shutdown();

        }

        private DispatcherTimer timer = new DispatcherTimer();
        public MainWindow()
        {
            //new ParkWindow().Show();
            //new LocationWindow().Show();
            // new User().Show();
            //new Order().Show();
            SystemManager.InitSystem();
            InitializeComponent();
            //this.Content = new MainPage();
            notice.ToolTip = $"服务地址：{SystemManager.Address}";
            timer.Tick += Timer_Tick;
            timer.Interval = TimeSpan.FromSeconds(0.2);
        }
        private void login_Click(object sender, RoutedEventArgs e)
        {
            time = 0;
            timer.Start();
            if (this.passwordbox.Password.Length > 0)
            {
                string token = passwordbox.Password;
                Request<bool>(s_Auth, null, token, delegate (Object obj)
                {
                    timer.Stop();
                    if (obj != null)
                    {
                        if ((bool)obj)
                        {
                            notice.Content = "登录成功";
                            Admin = passwordbox.Password;
                            Thread.Sleep(500);
                            this.Content = new MainPage();
                        }
                        else
                            notice.Content = "登录失败";
                    }
                    else
                    {
                        notice.Content = "网络错误，请检查网络";
                        MessageBox.Show("网络连接错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }


                }, delegate ()
                {
                    notice.Content = "请稍等...";
                });
            }
            else
            {
                MessageBox.Show("请先输入您的管理员用户密钥", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        int time = 0;
        private void Timer_Tick(object sender, EventArgs e)
        {
            Dispatcher.Invoke(delegate ()
            {
                notice.Content = $"请稍等...{time++ * 0.2}";
            });
        }

        private void showpassword_Checked(object sender, RoutedEventArgs e)
        {
            passwordbox.IsShow = true;
        }

        private void showpassword_Unchecked(object sender, RoutedEventArgs e)
        {
            passwordbox.IsShow = false;

        }
    }
}
